package role

import (
	"context"
	"github.com/jinzhu/copier"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InfoLogic) RoleInfo(req *types.RoleInfoDetailReq) (resp *types.RoleInfoDetailResp, err error) {
	// 1.builder rpc request
	detailReq := pb.RoleInfoDetailReq{
		RoleId: req.RoleId,
	}
	// 2.request rpc
	roleInfo, err := l.svcCtx.UserCenterClient.RoleInfo(l.ctx, &detailReq)
	if err != nil {
		logx.Error("query role info from user-rpc failed:%s", err.Error())
		return nil, err
	}
	// 3.return obj
	resp = new(types.RoleInfoDetailResp)
	copyErr := copier.Copy(resp, roleInfo)
	if copyErr != nil {
		return nil, copyErr
	}
	resp.DeptIdArr = roleInfo.DeptIdArr
	return resp, nil
}
